package models

import (
	"fmt"
	"rongyy/src/app"
	"rongyy/src/middlewares"
)

type PatientXyjyModel struct {
	BaseModel   `xorm:"extends"`
	AgreementNo string `json:"agreementNo"`
	PatientId   string `json:"patient_id"`
}

type XyjyReqModel struct {
	PayId       string `json:"payId"`
	PayFee      string `json:"payFee"`
	AgreementNo string `json:"agreementNo"`
	Nme         string `json:"nme"`
	IdNo        string `json:"idNo"`
	BillNo      string `json:"billNo"`
	NotifyUrl   string `json:"notifyUrl"`
	PatientId   string `json:"patient_id"`
}

type XyjyAgreementRespModel struct {
	Success        bool        `json:"success"`
	BizState       string      `json:"biz_state"`
	RspCode        string      `json:"rsp_code"`
	RspMsg         interface{} `json:"rsp_msg"`
	RefMsgId       interface{} `json:"ref_msg_id"`
	AgreementNo    string      `json:"agreement_no"`
	UseId          string      `json:"use_id"`
	CreditAmt      string      `json:"credit_amt"`
	SignTime       string      `json:"sign_time"`
	AgreementSts   string      `json:"agreement_sts"`
	ChannelId      string      `json:"channel_id"`
	DiscountFlag   string      `json:"discount_flag"`
	DiscountName   interface{} `json:"discount_name"`
	DiscountDetail interface{} `json:"discount_detail"`
	DiscountUrl    interface{} `json:"discount_url"`
	ExtFld         string      `json:"ext_fld"`
}

type XyjyPayRespModel struct {
	Success            bool        `json:"success"`
	BizState           string      `json:"biz_state"`
	RspCode            string      `json:"rsp_code"`
	RspMsg             string      `json:"rsp_msg"`
	RefMsgId           interface{} `json:"ref_msg_id"`
	PaySts             string      `json:"pay_sts"`
	PayTime            string      `json:"pay_time"`
	PaySqn             string      `json:"pay_sqn"`
	PayId              string      `json:"pay_id"`
	DiscountFee        string      `json:"discount_fee"`
	ActualFee          string      `json:"actual_fee"`
	PayFee             string      `json:"pay_fee"`
	DiscountFailReason interface{} `json:"discount_fail_reason"`
	ExtFld             string      `json:"ext_fld"`
}

type XyjyRefundReqModel struct {
	AppId          string `json:"appId"`
	MyPrivateKey   string `json:"myPrivateKey"`
	ApigwPublicKey string `json:"apigwPublicKey"`
	RefundId       string `json:"refundId"`
	PayId          string `json:"payId"`
	RefundFee      string `json:"refundFee"`
}

type XyjyRefundRespModel struct {
	Success           bool        `json:"success"`
	BizState          string      `json:"biz_state"`
	RspCode           string      `json:"rsp_code"`
	RspMsg            interface{} `json:"rsp_msg"`
	RefMsgId          interface{} `json:"ref_msg_id"`
	RefundSts         string      `json:"refund_sts"`
	RefundFee         string      `json:"refund_fee"`
	RefundId          string      `json:"refund_id"`
	DiscountRefundFee string      `json:"discount_refund_fee"`
	ActualRefundFee   string      `json:"actual_refund_fee"`
	RefundTime        string      `json:"refund_time"`
	RefundSqn         string      `json:"refund_sqn"`
	Extfld            string      `json:"extfld"`
}

type XyjyCheckRespModel struct {
	Success  bool        `json:"success"`
	BizState string      `json:"biz_state"`
	RspCode  string      `json:"rsp_code"`
	RspMsg   interface{} `json:"rsp_msg"`
	RefMsgId interface{} `json:"ref_msg_id"`
	LimitFlg string      `json:"limit_flg"`
	ExtFld   string      `json:"ext_fld"`
}

func (pm *PatientXyjyModel) TableName() string {
	return "pat_xyjy"
}

func (rm *PatientXyjyModel) Insert() (*PatientXyjyModel, error) {
	_, err := app.DB().Insert(rm)
	if err != nil {
		middlewares.Logger.Println("insert user fail", err)
		return nil, err
	}
	return rm, err
}

func (rm *PatientXyjyModel) Get(pid string) (*PatientXyjyModel, error) {
	pm := &PatientXyjyModel{}
	has, err := app.DB().Table("pat_xyjy").Where("patient_id=? ", pid).Get(pm)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, fmt.Errorf("签约信息不存在")
	}
	return pm, err
}
